package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/7/23 13:49
 * @apiNote
 */

public class Test00997NumRookCaptures {

    public int numRookCaptures1(char[][] board) {
        int cnt = 0, st = 0, ed = 0;
        int[] dx = {0, 1, 0, -1};
        int[] dy = {1, 0, -1, 0};
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == 'R') {
                    st = i;
                    ed = j;
                    break;
                }
            }
        }
        for (int i = 0; i < 4; i++) {
            for (int step = 0; ; step++) {
                int tx = st + step * dx[i];
                int ty = ed + step * dy[i];
                if (tx < 0 || tx >= 8 || ty < 0 || ty >= 8 || board[tx][ty] == 'B') {
                    break;
                }
                if (board[tx][ty] == 'p') {
                    cnt++;
                    break;
                }
            }
        }
        return cnt;
    }

    public int numRookCaptures(char[][] board) {
        int x = 0;
        int y = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 'R') {
                    x = i;
                    y = j;
                    break;
                }
            }
        }
        int count = 0;
        for (int i = x - 1; i >= 0; i--) {
            if (board[i][y] == 'B') {
                break;
            }
            if (board[i][y] == 'p') {
                count++;
                break;
            }
        }
        for (int i = x + 1; i < board[0].length; i++) {
            if (board[i][y] == 'B') {
                break;
            }
            if (board[i][y] == 'p') {
                count++;
                break;
            }
        }
        for (int i = y - 1; i >= 0; i--) {
            if (board[x][i] == 'B') {
                break;
            }
            if (board[x][i] == 'p') {
                count++;
                break;
            }
        }
        for (int i = y + 1; i < board.length; i++) {
            if (board[x][i] == 'B') {
                break;
            }
            if (board[x][i] == 'p') {
                count++;
                break;
            }
        }
        return count;
    }
}
